package core.process.pack;

import core.rb.PB;
import java.util.concurrent.CountDownLatch;
import core.sb.SB;
import gui.processor.Processor;
import gui.utils.ProgBar;

/*Author: John Archer, Contact: john.archer.jpa@gmail.com*/
public class NucsPack extends Thread{
    private final CountDownLatch stopLatch;
    protected PB pIn;
    protected int [][] pOut;
    private Processor gp;
    private double filter;

    public NucsPack(Processor gp, CountDownLatch sl, PB pack, double f){
        super();
        this.gp = gp;
        this.pIn = pack;
        this.stopLatch = sl;
        this.filter = f;
        this.start();
    }
    public void run(){
        pOut = getNucs();
        stopLatch.countDown();
    }
    public int [][] getPackOut(){return pOut;}

    private int [][] getNucs() {
        int [][] freqs = new int[SB.NUCS.length][pIn.getRef().getDNA().length()];
        if(gp!=null){
            String dna;
            gp.dPrg(ProgBar.START, SB.NUCS.length);
            for(int k=0; k<SB.NUCS.length; k++){
                int [] noPerCol = new int[pIn.getRef().getDNA().length()];
                 for(int z=0; z<pIn.getSize(); z++){
                    if(this.filter==-1){dna = pIn.getScDNA(z);}
                    else{dna = pIn.getScDNA(z, filter);}
                    for(int t=pIn.getRead(z).getStart(); t<pIn.getRead(z).getEnd() && t<pIn.getRef().getDNA().length(); t++){
                        if(dna.charAt(t-pIn.getRead(z).getStart()) == SB.NUCS[k]){noPerCol[t]++;}
                    }
                }
                for(int t=0; t<noPerCol.length; t++){freqs[k][t] = noPerCol[t];}
                gp.dPrg(ProgBar.UPDATE, k);
            }
            gp.dPrg(ProgBar.END, 0);
        }
        else{
            String dna;
            for(int k=0; k<SB.NUCS.length; k++){
                int [] noPerCol = new int[pIn.getRef().getDNA().length()];
                 for(int z=0; z<pIn.getSize(); z++){
                    if(this.filter==-1){dna = pIn.getScDNA(z);}
                    else{dna = pIn.getScDNA(z, filter);}
                    for(int t=pIn.getRead(z).getStart(); t<pIn.getRead(z).getEnd() && t<pIn.getRef().getDNA().length(); t++){
                        if(dna.charAt(t-pIn.getRead(z).getStart()) == SB.NUCS[k]){noPerCol[t]++;}
                    }
                }
                for(int t=0; t<noPerCol.length; t++){freqs[k][t] = noPerCol[t];}
            }
        }
        return freqs;
    }
}